package com.zz.inventory.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;

import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.StudentExamRecords;
import com.zz.inventory.service.IStudentExamRecordsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学生考试记录Controller
 * 
 * @author ruoyi
 * @date 2025-07-03
 */
@RestController
@RequestMapping("/records")
public class StudentExamRecordsController extends BaseController
{
    @Autowired
    private IStudentExamRecordsService studentExamRecordsService;

    /**
     * 查询学生考试记录列表
     */
    @RequiresPermissions("Teacher:records:list")
    @GetMapping("/list")
    public TableDataInfo list(StudentExamRecords studentExamRecords)
    {
        startPage();
        List<StudentExamRecords> list = studentExamRecordsService.selectStudentExamRecordsList(studentExamRecords);
        return getDataTable(list);
    }

    /**
     * 导出学生考试记录列表
     */
    @RequiresPermissions("Teacher:records:export")
    @Log(title = "学生考试记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentExamRecords studentExamRecords)
    {
        List<StudentExamRecords> list = studentExamRecordsService.selectStudentExamRecordsList(studentExamRecords);
        ExcelUtil<StudentExamRecords> util = new ExcelUtil<StudentExamRecords>(StudentExamRecords.class);
        util.exportExcel(response, list, "学生考试记录数据");
    }

    /**
     * 获取学生考试记录详细信息
     */
    @RequiresPermissions("Teacher:records:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(studentExamRecordsService.selectStudentExamRecordsByRecordId(recordId));
    }

    /**
     * 新增学生考试记录
     */
    @RequiresPermissions("Teacher:records:add")
    @Log(title = "学生考试记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentExamRecords studentExamRecords)
    {
        return toAjax(studentExamRecordsService.insertStudentExamRecords(studentExamRecords));
    }

    /**
     * 修改学生考试记录
     */
    @RequiresPermissions("Teacher:records:edit")
    @Log(title = "学生考试记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentExamRecords studentExamRecords)
    {
        return toAjax(studentExamRecordsService.updateStudentExamRecords(studentExamRecords));
    }

    /**
     * 删除学生考试记录
     */
    @RequiresPermissions("Teacher:records:remove")
    @Log(title = "学生考试记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(studentExamRecordsService.deleteStudentExamRecordsByRecordIds(recordIds));
    }

    // 查询学生关联的考试记录（从 student_exam_records 表）,远程调用
    @GetMapping("/students/{studentId}")    //步骤2，检查有没有记录
    public AjaxResult getByStudentId(@PathVariable Long studentId) {
        // 1. 显式检查学生是否存在
        if (studentId == null || studentId <= 0) {
            return AjaxResult.error("无效的学生ID");
        }

       // 检查是否存在
        List<StudentExamRecords> records = studentExamRecordsService.getByStudentId(studentId);

        Map<String, Object> result = new HashMap<>();
        result.put("hasAssociatedRecords", !records.isEmpty());
        result.put("count", records.size());
        result.put("records", records);

        // 3. 添加调试日志
        logger.debug("学生[{}]考试记录查询结果: {}", studentId, records.size());

        return AjaxResult.success(result);
    }

    // 删除学生关联的考试记录（从 student_exam_records 表）
    @DeleteMapping("/students/{studentId}")
    public AjaxResult deleteByStudentId(@PathVariable Long studentId) {
        int rows = studentExamRecordsService.deleteByStudentId(studentId);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error("删除关联记录失败");
    }
}
